import { UseFilters } from '@nestjs/common';
import { Args, Int, Mutation, Resolver } from '@nestjs/graphql';
import { GrpcFilter } from 'src/common/grpc/grpc.filter';
import { Problem } from './problem.entity';
import { GrpcService } from 'src/common/grpc/grpc.service';

@UseFilters(GrpcFilter)
@Resolver(() => Problem)
export class ProblemResolver {
  constructor(private readonly grpcService: GrpcService) {}

  @Mutation(() => Problem)
  async createProblem(
    @Args('name') name: string,
    @Args('categoryId', { type: () => Int }) categoryId: number,
    @Args('content') content: string,
    @Args('verifyCode') verifyCode: string,
  ): Promise<Problem> {
    const pbp = await this.grpcService.service
      .createProblem({ ID: 0, name, categoryId, content, verifyCode })
      .toPromise();
    const p: Problem = { _id: pbp.ID, ...pbp };
    return p;
  }

  @Mutation(() => Boolean)
  async removeProblem(@Args('_id') _id: number): Promise<boolean> {
    this.grpcService.service.removeProblem({ ID: _id });
    return true;
  }
}
